package cn.bdqn.service.impl;

import cn.bdqn.mapper.ItripHotelOrderMapper;
import cn.bdqn.pojo.ItripHotelOrder;
import cn.bdqn.service.OrderService;
import cn.bdqn.util.EmptyUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 2018/8/29
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private ItripHotelOrderMapper itripHotelOrderMapper;
    @Override
    public ItripHotelOrder loadItripHotelOrder(String orderNo) throws Exception {
        // 加载酒店订单
       List<ItripHotelOrder> itripHotelOrderList= itripHotelOrderMapper.getOrderNoList(orderNo);
       if (itripHotelOrderList.size()>0){
           return itripHotelOrderList.get(0);
       }else {
           return null ;
       }

    }

    @Override
    public void paySuccess(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        //orderStatus  订单状态  0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评
        // payType  支付方式:1:支付宝 2:微信 3:到店付

        itripHotelOrder.setOrderStatus(2);
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);
        this.itripHotelOrderMapper.updateByPrimaryKeySelective(itripHotelOrder);

        //减库存 由业务模块提供API 在这里进行调用


    }

    @Override
    public void payFailed(String orderNo, int payType, String tradeNo) throws Exception {
        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        //orderStatus  订单状态  0：待支付 1:已取消 2:支付成功 3:已消费 4：已点评
        // payType  支付方式:1:支付宝 2:微信 3:到店付

        itripHotelOrder.setOrderStatus(1);
        itripHotelOrder.setPayType(payType);
        itripHotelOrder.setTradeNo(tradeNo);
        this.itripHotelOrderMapper.updateByPrimaryKeySelective(itripHotelOrder);
    }

    @Override
    public boolean processed(String orderNo) throws Exception {

        ItripHotelOrder itripHotelOrder=this.loadItripHotelOrder(orderNo);
        return itripHotelOrder.getOrderStatus().equals(2)&&!EmptyUtils.isEmpty(itripHotelOrder.getTradeNo());
    }
}
